var util = require('util.js')
var md5 = require('md5.js')
var appid = 'wxaaf5f79c147f6f2e'
var key = '60dd010d4454a100b032931e93eef052'

//调起支付接口
/* options 至少三个参数 
    tenantid //租户ID
    openid   //用户openid
    fee      //支付金额
    body     //购买内容，可以是标题
  */
function pay(options, success, fail) {
  var timestamp = (new Date().getTime()).toString()
  // var rand_str = util.randomStr(32)
  var nonceStr = options.nonceStr //唯一标识 
  console.log(options)
  util.getPayId(options.t, options.open_id, options.fee, options.detail, nonceStr, function (res) {
    var str = "appId=" + appid + "&nonceStr=" + nonceStr + "&package=prepay_id=" + res.data.data.payid + "&signType=MD5&timeStamp=" + timestamp + "&key=" + key
    var paySign = md5.hexMD5(str).toUpperCase()
    wx.requestPayment({
      'timeStamp': timestamp,
      'nonceStr': nonceStr,
      'package': 'prepay_id=' + res.data.data.payid,
      'signType': 'MD5',
      'paySign': paySign,
      'success': success,
      'fail': fail
    })
  })
}

module.exports = {
  pay: pay
}